package Classes
{
	import Classes.Components.AIComponent;
	import Classes.Components.ActionComponent;
	import Classes.Components.IComponent;
	import Classes.Components.PlayerComponent;
	import Classes.Components.RigidBodyComponent;
	import Classes.Components.SkillComponent;
	
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.utils.getQualifiedClassName;

	public class Entity extends EventDispatcher
	{
		public var name:String;
		public var config:Object;
		public var desc:String;
		public var dead:Boolean = false;
		
		private var m_components:Object = new Object;
		private var m_world:World;
		
		public function Entity(name:String){
			this.name = name;
			config = Main.configManager.getEntityByName(name);
		}
		
		public function getWorld():World{
			return m_world;
		}

		public function setWorld(world:World):void{
			m_world = world;
		}
		
		public function hasTag(tag:String):Boolean{
			if(!config.children.tags){
				return false;
			}
			
			for each(var tagConfig:Object in config.children.tags[0].children.tag){
				if(tagConfig.attributes['value'] == tag){
					return true;
				}
			}
			return false;
		}
		
		public function getColor():uint{
			var color:Number = Number(config.attributes['color']);
			return color;
		}
		
		public function addComponent(component:IComponent):Entity{
			m_components[getQualifiedClassName(component)] = component;
			Main.componentManager.addComponent(component);
			return this;
		}

		public function getRigidBodyComponent():RigidBodyComponent{
			return m_components[getQualifiedClassName(Main.prototypes.rigidBodyComponent)] as RigidBodyComponent;			
		}
		
		public function getAIComponent():AIComponent{
			return m_components[getQualifiedClassName(Main.prototypes.aiComponent)] as AIComponent;			
		}	
		
		public function getActionComponent():ActionComponent{
			return m_components[getQualifiedClassName(Main.prototypes.actionComponent)] as ActionComponent;			
		}	
		
		public function getSkillComponent():SkillComponent{
			return m_components[getQualifiedClassName(Main.prototypes.skillComponent)] as SkillComponent;			
		}
		
		public function getPlayerComponent():PlayerComponent{
			return m_components[getQualifiedClassName(Main.prototypes.playerComponent)] as PlayerComponent;			
		}			
		
		public function destroy():void{
			for each(var component:IComponent in m_components){
				component.enableDestroy();
			}
		}
	}
}